<template>
  <div class='orders_container'>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)"></el-table-column>
        <el-table-column label="是否支付">
          <template scope="{ row }">
            <el-tag type="success" v-if="row.pay_status !== '0'">已支付</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
          <template scope="{ row }">
            <el-tag type="danger" v-if="row.is_send === '否'">未发货</el-tag>
            <el-tag type="success" v-else>已发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间">
          <template scope="{ row }">
            <span>{{ row.create_time | dateFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template>
            <el-button type="primary" icon="el-icon-edit" circle @click="showBox"></el-button>
            <el-button type="success" icon="el-icon-location" circle @click="showProgressBox"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%" @close="addressDialogClosed">
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          <span>{{activity.context}}</span>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import orders from '@/api/orders'
import cityData from './citydata'

export default {
  name: 'MyOrders',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      orderList: [],
      total: 0,
      addressVisible: false,
      progressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'change' }],
        address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      cityData,
      progressInfo: []
    }
  },
  components: {},
  methods: {
    // 1.获取订单列表
    async getOrderList () {
      const { data: res } = await orders.getOrders(this.queryInfo)
      if (res.meta.status === 200) {
        this.orderList = res.data.goods
        this.total = res.data.total
      }
    },

    // 2.更换分页大小
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },

    // 3.更换当前页
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },

    // 4.显示修改地址的对话框
    showBox () {
      this.addressVisible = true
    },

    // 5.对话框关闭
    addressDialogClosed () {
      this.$refs.addressFormRef.resetFields()
    },

    // 6.确认修改地址[假数据]
    editAddress () {
      this.$refs.addressFormRef.validate(valid => {
        if (valid) {
          this.addressVisible = false
          this.$message.success('修改成功')
        } else {
          return false
        }
      })
    },

    // 查看物流信息
    showProgressBox () {
      const res = JSON.parse(orders.getProgressInfo('1106975712662'))
      if (res.meta.status === 200) {
        this.progressInfo = res.data
      } else {
        return this.$message.error(res.meta.msg)
      }
      this.progressVisible = true
    }
  },
  created () {
    this.getOrderList()
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
@import url('../../plugins/timeline/timeline.css');
@import url('../../plugins/timeline-item/timeline-item.css');

.el-breadcrumb {
  line-height: 50px;
}

.el-table {
  margin: 15px 0;
}

.el-cascader {
  width: 100%;
}
</style>
